package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.dexilaonian.MainActivity;
import com.dexilaonian.R;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.WorksSizeCheckUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class RegisterActivity_pwdActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.btn_pwd)
    Button btnPwd;
    @Bind(R.id.add)
    ImageView add;
    @Bind(R.id.set_pwd)
    EditText setPwd;
    @Bind(R.id.ensure_pwd)
    EditText ensurePwd;
    private String set_pwd;
    private String ensure_pwd;
    private String url;
    private String myphone;
    @Override
    protected void initData() {
    }
    @Override
    public int getLayoutID() {
        return R.layout.activity_register_pwd;
    }
    @Override
    public void initView() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        url = Constant.base_url + "/user/registerByPhone";
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        myphone = extras.getString("str");

        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(btnPwd);
        textChangeListener.addAllEditText(setPwd,ensurePwd);
        WorksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    btnPwd.setTextColor(getResources().getColor(R.color.white));
                    btnPwd.setBackground(getResources().getDrawable(R.drawable.btn_login2));
                } else {
                    btnPwd.setTextColor(getResources().getColor(R.color.gray));
                    btnPwd.setBackground(getResources().getDrawable(R.drawable.btn_login1));
                }
            }
        });

    }

    @OnClick({R.id.back, R.id.btn_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_pwd:
                set_pwd = setPwd.getText().toString().trim();
                ensure_pwd = ensurePwd.getText().toString().trim();
                if (set_pwd.length()<6 || set_pwd.length() >19) {
                    Toast.makeText(RegisterActivity_pwdActivity.this, "密码长度不正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (set_pwd.equals(ensure_pwd)) {
                    registerpwd(myphone,ensure_pwd);
                } else {
                    Toast.makeText(this, "两次输入密码不同", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    private void registerpwd(String myphone1, String ensure_pwd) {
        OkHttpUtils.post().url(url).addParams("phone", myphone1).addParams("password",ensure_pwd).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(RegisterActivity_pwdActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                Log.e("TTT", e.getMessage());
            }
            @Override
            public void onResponse(String response, int id) {
                Log.e("TTTt",response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 2) {
                        showToast("手机号重复注册");
                        return;
                    }
                    if (status == 0) {
                        String money = jsonObject.optString("money");
                        String surplusQuota = jsonObject.optString("surplusQuota");
                        String messages = jsonObject.optString("messages");
                        String userName = jsonObject.optString("userName");
                        String loanStatusStr = jsonObject.optString("loanStatusStr");
                        startActivity(new Intent(RegisterActivity_pwdActivity.this, MainActivity.class));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
